package com.zhongketech.sunflower;


import com.umeng.fb.FeedbackAgent;
import com.zhongketech.sunflower.database.ApplicationData;
import com.zhongketech.sunflower.database.Data;
import com.zhongketech.sunflower.database.PhoneNumberData;
import com.zhongketech.sunflower.droidwall.MainActivity;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Root;
import com.zhongketech.sunflower.util.SystemUiHider;
import com.zhongketech.sunflower.util.Utilities;
import com.zhongketech.sunflower.view.LogUpdate;
import com.zhongketech.sunflower.view.UninstallList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;



public class FullscreenActivity extends BaseActivity {

	private final static String TAG = "FullscreenActivity";
	private static Context context = null;

	private MyHandler myHandler = null;



	/**
	 * View init
	 */
	private ListView logListView = null;
	private Button uninstallAppButton = null;
	private Button phoneNumButton = null;
	private Button netButton = null;
	private Button appStartButton = null;
	private Button smsButton = null;
	private Button lockStartButton = null;
	private Button fbButton = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_fullscreen);

		//custom
		myHandler = new MyHandler();
		Utilities.myHandler = this.myHandler;
		initView();
		new rootTask().execute();
		//		Data.getinstance().add(new PhoneNumberData("1920000000", "chen"));
		//		Data.getinstance().add(new ApplicationData("com.coship.xxx", "测试"));
	}

	private void initView()
	{
		context = this;
		logListView = (ListView) findViewById(R.id.loglistview);
		logListView.setAdapter(LogUpdate.getInstance(this).getAdapter());
		uninstallAppButton = (Button) findViewById(R.id.app_button);
		uninstallAppButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
				intent.setClass(FullscreenActivity.this, UninstallAppActivity.class);  
				startActivity(intent);  
			}
		});
		phoneNumButton = (Button) findViewById(R.id.num_button);
		phoneNumButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
				intent.setClass(FullscreenActivity.this, PhoneNumActivity.class);  
				startActivity(intent);  
			}
		});

		netButton = (Button) findViewById(R.id.netbutton);
		netButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
				intent.setClass(FullscreenActivity.this, MainActivity.class);  
				startActivity(intent);  
			}
		});


		appStartButton = (Button) findViewById(R.id.appstartbutton);
		appStartButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
				intent.setClass(FullscreenActivity.this, StartAppActivity.class);  
				startActivity(intent);  
			}
		});

		lockStartButton = (Button) findViewById(R.id.lockbutton);
		lockStartButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
				intent.setClass(FullscreenActivity.this, LockActivity.class);  
				startActivity(intent);  
			}
		});

		fbButton = (Button) findViewById(R.id.fbbutton);

		final FeedbackAgent agent = new FeedbackAgent(context);
		agent.sync();
		fbButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				agent.startFeedbackActivity(); 
			}
		});


	}


	/**
	 * 数据 更新操作
	 */
	static class MyHandler extends Handler
	{


		@Override
		public void handleMessage(Message msg) 
		{

			//Utilities.logCat(TAG, "msg.what "+msg.what+" msg.arg1: "+msg.arg1+" msg.arg2: "+msg.arg2);
			switch (msg.what) {
			case Constants.UPDATE_LOG:
				LogUpdate.getInstance(context).update((String) msg.obj,msg.arg1);
				break;

			default:
				break;
			}
		}

	}

	/**
	 * 开启线程去获取root权限
	 * 
	 */
	class rootTask extends AsyncTask<String, Object, Integer> 
	{
		private String pkgName = "";

		@Override
		public Integer doInBackground(String... params) {
			Integer result = 1;
			boolean isRoot = Root.preparesu();
			if(isRoot)
			{
				result = 1;
			}
			else
			{
				result = 0;
			}
			return result;
		}

		@Override
		public void onPostExecute(Integer result) {
			if(1 == result)
			{
				Utilities.logCat(TAG, "已获取ROOT权限",Constants.LOG_INFO);
			}
			else
			{
				Utilities.logCat(TAG, "获取ROOT权限失败",Constants.LOG_INFO);
			}

			super.onPostExecute(result);
		}

	}


}
